package com.lightjet.macross.base.upms.service.impl;

import com.lightjet.macross.base.upms.domain.ResourceInfo;
import com.lightjet.macross.base.upms.domain.RoleInfo;
import com.lightjet.macross.base.upms.domain.RoleResourceLink;
import com.lightjet.macross.base.upms.domain.SystemInfo;
import com.lightjet.macross.base.upms.repository.ResourceInfoRepository;
import com.lightjet.macross.base.upms.service.ResourceInfoService;
import com.lightjet.macross.base.upms.service.RoleResourceLinkService;
import com.lightjet.macross.base.upms.service.RoleUserLinkService;
import com.lightjet.macross.base.upms.service.SystemInfoService;
import com.lightjet.macross.common.core.BaseServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResourceInfoServiceImpl extends BaseServiceImpl<ResourceInfo,Integer> implements ResourceInfoService {

    @Resource
    SystemInfoService systemInfoService;

    @Resource
    RoleUserLinkService roleUserLinkService;

    @Resource
    RoleResourceLinkService roleResourceLinkService;

    @Override
    @Cacheable(value = "user", key = "'resource_userid:' + #userkey + '@' + #systemshortname", unless = "#result eq null")
    public List<ResourceInfo> GetUserResourceCollection(String userkey, String systemshortname) {
        List<ResourceInfo> resourceInfoList = new ArrayList<>();
        SystemInfo systemInfo = systemInfoService.FindByShortname(systemshortname);
        if(systemInfo!=null)
        {
            List<RoleInfo> list = roleUserLinkService.FindBySystemAndUserWithAnybody(systemInfo,userkey);
            for(RoleInfo roleInfo : list)
            {
                List<ResourceInfo> resList = roleResourceLinkService.FindByRoleinfo(roleInfo).stream().map(RoleResourceLink::getResourceInfo).collect(Collectors.toList());
                for (ResourceInfo resourceInfo : resList)
                {
                    if(!resourceInfoList.contains(resourceInfo))
                    {
                        resourceInfo.setChildren(null);
                        resourceInfoList.add(resourceInfo);
                    }
                }
            }
        }
        return resourceInfoList;
    }

    /**
     * Description //根据资源父节点及所属租户系统查询树状关系
     * @param systemInfo   组合系统对象
     * @return java.util.List<com.lightjet.macross.base.upms.domain.ResourceInfo>
     * CreateTime 2020/3/11 9:10
     * @author lijia
     * @Param resourceInfo 资源节点对象
     **/
    @Override
    public List<ResourceInfo> FindAllByParentAndBelongsystem(ResourceInfo resourceInfo, SystemInfo systemInfo) {
        return ((ResourceInfoRepository) baseRepository).findAllByParentAndBelongsystem(resourceInfo,systemInfo);
    }

    /**
     * Description //根据资源ID获取唯一对象
     *
     * @param id
     * @return com.lightjet.macross.base.upms.domain.ResourceInfo
     * CreateTime 2020/3/11 16:18
     * @author lijia
     * @Param id
     */
    @Override
    public ResourceInfo FindDistinctById(Integer id) {
        return ((ResourceInfoRepository) baseRepository).findDistinctById(id);
    }


}
